package com.ubi.core.framework.response;


import java.io.Serializable;

@SuppressWarnings("serial")
public class Result<T> implements Serializable {
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static final Result SUCCESS = new Result(Code.SUCCESS.getCode(), null, null);

	public static final String defaultError="服务器忙请稍后再试！";

	public enum Code {
		SUCCESS(200,"成功"), ERROR(-400,"异常");
		private int code;
		private String desc;
		Code(int code,String desc){
			this.code=code;
			this.desc=desc;
		}

		public int getCode() {
			return code;
		}

		public void setCode(int code) {
			this.code = code;
		}

		public String getDesc() {
			return desc;
		}

		public void setDesc(String desc) {
			this.desc = desc;
		}
	}

	private int code = 200;
	private String message;
	private T data;

	public Result() {
	}

	public Result(int code, String message, T data) {
		this.code = code;
		this.message = message;
		this.data = data;
	}

	public Result(T data) {
		this.code = Code.SUCCESS.getCode();
		this.data = data;
	}

	public static <T> Result<T> newSuccess(T data) {
		return new Result<>(Code.SUCCESS.getCode(), "", data);
	}

	public static Result newFailed(String message) {
		return new Result(Code.ERROR.getCode(), message, null);
	}

	public static <T> Result<T> newFailed(String message, T data) {
		return new Result(Code.ERROR.getCode(), message, data);
	}


	public int getCode() {
		return code;
	}

	public void setCode(Code code) {
		this.code = code.getCode();
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	public void doErrorHandle(String message) {
		this.code = Code.ERROR.getCode();
		this.message = message;
	}

	public void doDefaultErrorHandle(){
		this.code = Code.ERROR.getCode();
		this.message = defaultError;
	}
}
